package com.jt.thread;
//J.U.C

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author 金帮嘉诚 @date 2021-08-20 星期五 17:49
 */
public class ThreadPoolTests {
    public static void main(String[] args) {
//         public ThreadPoolExecutor(int corePoolSize,
//        int maximumPoolSize,
//        long keepAliveTime,
//        TimeUnit unit,
//        BlockingQueue<Runnable> workQueue)
        //1.核心线程数
        int corePoolSize = 2;
        //2.最大线程数
        int maximumPoolSize = 3;
        //3.线程最大空闲时间
        long keepAliveTime = 60;
        //4.时间单位
        TimeUnit unit = TimeUnit.SECONDS;
        //5.任务队列
        BlockingQueue<Runnable> workQueue =
                new ArrayBlockingQueue<>(1);

        //线程取名
        String preName = "cgb2105-thread-";
        AtomicInteger count = new AtomicInteger(1);

//        ThreadFactory threadFactory=new ThreadFactory() {
//            @Override
//            public Thread newThread(Runnable r) {
//                String tName = preName+count.getAndIncrement();
//                return new Thread(r,tName);
//            }
//        };
        ThreadFactory threadFactory = (r) -> {
            String tName = preName + count.getAndIncrement();
            return new Thread(r, tName);

        };
        //异常处理
        RejectedExecutionHandler handler = (r, executor) -> {
            System.out.println(Thread.currentThread().getName()+"错误!!!!!!!");
        };


        //6.创建线程池
        ThreadPoolExecutor pool =
                new ThreadPoolExecutor(
                        corePoolSize,
                        maximumPoolSize,
                        keepAliveTime,
                        unit,
                        workQueue,
                        threadFactory,
                        handler);
        //7.将任务交给池中对象去执行
        pool.execute(() -> {
            String tName =
                    Thread.currentThread().getName();
            System.out.println(tName + "->任务1");
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
            }

        });
        pool.execute(() -> {
            String tName =
                    Thread.currentThread().getName();
            System.out.println(tName + "->任务2");
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
            }

        });
        pool.execute(() -> {
            String tName =
                    Thread.currentThread().getName();
            System.out.println(tName + "->任务3");
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
            }

        });
        pool.execute(() -> {

            String tName =
                    Thread.currentThread().getName();
            System.out.println(tName + "->任务4");
            try {
                Thread.sleep(5000);
            } catch (Exception e) {
            }

        });
        pool.execute(new Runnable() {
            @Override
            public void run() {
                String tName =
                        Thread.currentThread().getName();
                System.out.println(tName + "->任务5");
            }
        });
    }
}
